package com.ruoyi.manage.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.ListUtil;
import com.ruoyi.manage.organization.domain.OcBusinessFinancingOrder;
import com.ruoyi.manage.organization.domain.OcOrderLoan;
import com.ruoyi.manage.organization.mapper.OcOrderLoanMapper;
import com.ruoyi.manage.organization.service.IOcBusinessFinancingOrderService;
import com.ruoyi.manage.organization.service.IOcOrderLoanService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;


/**
 * 订单用信记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-19
 */
@Service
public class OcOrderLoanServiceImpl extends ServiceImpl<OcOrderLoanMapper, OcOrderLoan> implements IOcOrderLoanService {


    @Autowired
    IOcBusinessFinancingOrderService iOcBusinessFinancingOrderService;

    @Override
    public OcOrderLoan queryGetById(Long id) {
        OcOrderLoan ocOrderLoan = this.getById(id);
        return ocOrderLoan;
    }

    /**
     * 新增订单用信记录
     *
     * @param ocOrderLoan 订单用信记录
     * @return 结果
     */
    @Override
    public boolean addOcOrderLoan(OcOrderLoan ocOrderLoan) {
//        判断用信金额是否大于授信金额
        OcBusinessFinancingOrder financingOrder = iOcBusinessFinancingOrderService.getOne(new QueryWrapper<OcBusinessFinancingOrder>().eq("order_no", ocOrderLoan.getOrderNo()));

        BigDecimal currentLoadAmount = ocOrderLoan.getLoanAmount().add(financingOrder.getLoanAmount());
        if (currentLoadAmount.compareTo(financingOrder.getCreditAmount()) > 0) {
            throw new CustomException("用信金额大于授信金额，不可操作");
        }
        String orderState = financingOrder.getOrderState();
        if (currentLoadAmount.compareTo(financingOrder.getCreditAmount()) == 0 && currentLoadAmount.compareTo(BigDecimal.ZERO) > 0) {
            orderState = "5";
        }
        boolean save = this.save(ocOrderLoan);
        iOcBusinessFinancingOrderService.update(new UpdateWrapper<OcBusinessFinancingOrder>()
                .set("loan_amount", currentLoadAmount)
                .set("order_state", orderState)
                .set("loan_time", ocOrderLoan.getLoanTime())
                .eq("order_no", ocOrderLoan.getOrderNo()));

        return save;
    }

    /**
     * 修改订单用信记录
     *
     * @param ocOrderLoan 订单用信记录
     * @return 结果
     */
    @Override
    public boolean updateOcOrderLoan(OcOrderLoan ocOrderLoan) {
        return this.updateById(ocOrderLoan);
    }

    /**
     * 批量删除订单用信记录
     *
     * @param ids 需要删除的订单用信记录主键
     * @return 结果
     */
    @Override
    public boolean deleteOcOrderLoanByIds(Long[] ids) {
        return this.removeByIds(ListUtil.toList(ids));
    }

}
